package com.elder.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ElderHealthCheckupVO;
import com.elder.business.domain.ElderlyInfo;
import com.elder.business.domain.MemberInfo;
import com.elder.business.mapper.ElderlyInfoMapper;
import com.elder.business.mapper.MemberInfoMapper;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.bean.BeanUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elder.business.mapper.ElderHealthCheckupMapper;
import com.elder.business.domain.ElderHealthCheckup;
import com.elder.business.service.IElderHealthCheckupService;

/**
 * 老人健康体检Service业务层处理
 * 
 * @author zyk
 * @date 2023-11-26
 */
@Service
public class ElderHealthCheckupServiceImpl implements IElderHealthCheckupService 
{
    @Autowired
    private ElderHealthCheckupMapper elderHealthCheckupMapper;
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;
    /**
     * 查询老人健康体检
     * 
     * @param id 老人健康体检主键
     * @return 老人健康体检
     */
    @Override
    public ElderHealthCheckupVO selectElderHealthCheckupById(Long id)
    {
        ElderHealthCheckup v = elderHealthCheckupMapper.selectElderHealthCheckupById(id);
        ElderHealthCheckupVO elderVO = new ElderHealthCheckupVO();
        BeanUtils.copyProperties(v,elderVO);
        elderVO.setId(StringNewUtils.formatStr(v.getId()));
        elderVO.setElderId(StringNewUtils.formatStr(v.getElderId()));
        elderVO.setExamDate(Objects.isNull(v.getExamDate())?"":DateUtils.dateStr(v.getExamDate()));
        elderVO.setCheckTime(Objects.isNull(v.getExamDate())?"":DateUtils.dateStr(v.getExamDate()));
        elderVO.setHealthScore(StringNewUtils.formatStr(v.getHealthScore()));
        elderVO.setDoctorId(StringNewUtils.formatStr(v.getDoctorId()));
        return elderVO;
    }

    /**
     * 查询老人健康体检列表
     *
     * @return 老人健康体检
     */
    @Override
    public TableDataInfo selectElderHealthCheckupList(String elderName , String startTime , String endTime , Integer pageNum , Integer pageSize)
    {
        List<Long> elderIds = null;
        if(!StringNewUtils.isEmpty(elderName)){
            List<ElderlyInfo> elderinfoList = elderlyInfoMapper.getElderinfoList(elderName);
            if(CollectionUtils.isNotEmpty(elderinfoList)){
                elderIds = elderinfoList.stream().map(ElderlyInfo::getElderId).collect(Collectors.toList());
            }
        }
        Long dayStartTime = null;
        Long dayEndTime = null;
        if(!StringNewUtils.isEmpty(startTime) && !StringNewUtils.isEmpty(endTime)){
            dayStartTime = DateUtils.getDayStartTime(DateUtils.getTargetDateSeconds(startTime));
            dayEndTime = DateUtils.getDayEndTime(DateUtils.getTargetDateSeconds(endTime));
        }
        PageHelper.startPage(pageNum, pageSize);
        List<ElderHealthCheckup> list = elderHealthCheckupMapper.selectElderHealthCheckupList(elderIds, dayStartTime, dayEndTime);
        PageInfo<ElderHealthCheckup> pageInfo = new PageInfo<>(list);
        List<ElderHealthCheckup> checkupList = pageInfo.getList();
        //获取老人数据
        List<Long> elderIdList = checkupList.stream().map(ElderHealthCheckup::getElderId).collect(Collectors.toList());
        List<ElderlyInfo> elderInfoList = elderlyInfoMapper.getElderInfoByElderIds(elderIdList);
        Map<Long, String> elderMap = elderInfoList.stream().collect(Collectors.toMap(ElderlyInfo::getElderId, ElderlyInfo::getElderName));
        //获取成员信息
        List<Long> memberIds = checkupList.stream().map(ElderHealthCheckup::getDoctorId).collect(Collectors.toList());
        Map<Long, String> memberMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(memberIds)){
            List<MemberInfo> memberInfoList = memberInfoMapper.getMemberListByMemberIds(memberIds);
            memberMap = memberInfoList.stream().collect(Collectors.toMap(MemberInfo::getMemberId, MemberInfo::getMemberName));

        }
        Map<Long, String> finalMemberMap = memberMap;
        List<ElderHealthCheckupVO> objectList = checkupList.stream().map(v -> {
            ElderHealthCheckupVO elderVO = new ElderHealthCheckupVO();
            BeanUtils.copyProperties(v,elderVO);
            elderVO.setId(StringNewUtils.formatStr(v.getId()));
            elderVO.setElderId(StringNewUtils.formatStr(v.getElderId()));
            elderVO.setElderName(elderMap.get(v.getElderId()));
            elderVO.setExamDate(Objects.isNull(v.getExamDate())?"":DateUtils.dateStr(v.getExamDate()));
            elderVO.setHealthScore(StringNewUtils.formatStr(v.getHealthScore()));
            elderVO.setDoctorId(StringNewUtils.formatStr(v.getDoctorId()));
            elderVO.setDoctorName(finalMemberMap.get(v.getDoctorId()));
            return elderVO;
        }).collect(Collectors.toList());
        TableDataInfo data = new TableDataInfo();
        data.setCode(200);
        data.setRows(objectList);
        data.setTotal(pageInfo.getTotal());
        return data;
    }

    /**
     * 新增老人健康体检
     * 
     * @param elderHealthCheckup 老人健康体检
     * @return 结果
     */
    @Override
    public int insertElderHealthCheckup(ElderHealthCheckup elderHealthCheckup)
    {
        elderHealthCheckup.setId(SnowGenerator.getId());
        elderHealthCheckup.setExamDate(DateUtils.getTargetDateSeconds(elderHealthCheckup.getCheckTime()));
        elderHealthCheckup.setCreateTime(DateUtils.getLongTime());
        return elderHealthCheckupMapper.insertElderHealthCheckup(elderHealthCheckup);
    }

    /**
     * 修改老人健康体检
     * 
     * @param elderHealthCheckup 老人健康体检
     * @return 结果
     */
    @Override
    public int updateElderHealthCheckup(ElderHealthCheckup elderHealthCheckup)
    {
        elderHealthCheckup.setUpdateTime(DateUtils.getLongTime());
        elderHealthCheckup.setExamDate(DateUtils.getTargetDateSeconds(elderHealthCheckup.getCheckTime()));
        return elderHealthCheckupMapper.updateElderHealthCheckup(elderHealthCheckup);
    }

    /**
     * 批量删除老人健康体检
     * 
     * @param ids 需要删除的老人健康体检主键
     * @return 结果
     */
    @Override
    public int deleteElderHealthCheckupByIds(Long[] ids)
    {
        return elderHealthCheckupMapper.deleteElderHealthCheckupByIds(ids);
    }

    /**
     * 删除老人健康体检信息
     * 
     * @param id 老人健康体检主键
     * @return 结果
     */
    @Override
    public int deleteElderHealthCheckupById(Long id)
    {
        return elderHealthCheckupMapper.deleteElderHealthCheckupById(id);
    }
}
